package com.example.bingobox.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.Label;
import java.util.List;
import java.util.ArrayList;

public class TimeSpanAdapter extends RecyclerView.Adapter<TimeSpanAdapter.ViewHolder> {
    private List<Label.TimeSpan> timeSpans;
    private OnTimeSpanActionListener listener;

    public interface OnTimeSpanActionListener {
        void onLabelClick(Label.TimeSpan timeSpan);
        void onDelete(Label.TimeSpan timeSpan);
    }

    public TimeSpanAdapter(List<Label.TimeSpan> timeSpans, OnTimeSpanActionListener listener) {
        this.timeSpans = timeSpans != null ? timeSpans : new ArrayList<>();
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        try {
            View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_timespan, parent, false);
            return new ViewHolder(view);
        } catch (Exception e) {
            e.printStackTrace();
            // 创建一个空的视图作为后备方案
            View emptyView = new View(parent.getContext());
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ViewHolder(emptyView);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        try {
            Label.TimeSpan timeSpan = timeSpans.get(position);
            if (timeSpan != null) {
                holder.labelText.setText(timeSpan.getLabel() != null ? timeSpan.getLabel() : "");
                holder.timeText.setText(String.format("%02d:%02d-%02d:%02d",
                    timeSpan.getStartHour(), timeSpan.getStartMinute(),
                    timeSpan.getEndHour(), timeSpan.getEndMinute()));

                holder.labelText.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onLabelClick(timeSpan);
                    }
                });

                holder.deleteButton.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onDelete(timeSpan);
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return timeSpans != null ? timeSpans.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView labelText;
        TextView timeText;
        ImageButton deleteButton;

        ViewHolder(View view) {
            super(view);
            try {
                labelText = view.findViewById(R.id.text_label);
                timeText = view.findViewById(R.id.text_time);
                deleteButton = view.findViewById(R.id.btn_delete);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
} 